package controller

import (
	"context"
	"mini-app/config/redis"
	"mini-app/internal/app/web/service"
	"mini-app/internal/define"
	"time"

	"192.168.1.75/go-pkg/logx"
	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type User struct{}

// 微信登录获取openid【code置换】
func (User) Login(c *gin.Context) {
	code := c.Query("code")
	salesID := c.Query("sales_id")
	token, phone, err := service.User{}.Login(c.Request.Context(), code, salesID)
	response.JSON(c, gin.H{
		"token": token,
		"phone": phone,
	}, err)
}

// 获取手机号【code置换】
func (User) GetPhoneNumber(c *gin.Context) {
	openid := c.GetString("openid")
	code := c.Query("code")
	data, err := service.User{}.GetPhoneNumber(c.Request.Context(), openid, code)
	response.JSON(c, data, err)
}

// 用户信息
func (User) Profile(c *gin.Context) {
	openid := c.GetString("openid")
	data, err := service.User{}.Profile(c.Request.Context(), openid)
	response.JSON(c, data, err)
}

// 登录退出
func (User) Logout(c *gin.Context) {
	token := c.GetString("token")
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
	defer cancel()
	rdb := redis.NewDB()
	rdb.Del(ctx, define.MiniAppSessionOpenid+token)
	rdb.Del(ctx, define.MiniAppSessionKey+token)
	response.JSON(c, nil, nil)
}

// 用户注销
func (User) Del(c *gin.Context) {
	openid := c.GetString("openid")
	err := service.User{}.Del(c.Request.Context(), openid)
	response.JSON(c, nil, err)
}

// 用户信息编辑
func (User) Save(c *gin.Context) {
	var params struct {
		NickName      string `json:"nick_name"`      // 昵称
		Phone         string `json:"phone"`          // 手机
		Gender        int    `json:"gender"`         // 性别
		Birth         string `json:"birth"`          // 生日
		Address       string `json:"address"`        // 地区
		Email         string `json:"email"`          // 邮箱
		Hobby         string `json:"hobby"`          // 爱好
		Image         string `json:"image"`          // 头像
		UserName      string `json:"user_name"`      // 用户姓名
		TelNumber     string `json:"tel_number"`     // 联系电话
		PostalCode    string `json:"postal_code"`    // 邮编
		ProvinceName  string `json:"province_name"`  // 省份
		CityName      string `json:"city_name"`      // 城市
		CountyName    string `json:"county_name"`    // 区
		StreetName    string `json:"street_name"`    // 街道
		DetailAddress string `json:"detail_address"` // 详细地址
	}
	c.ShouldBindBodyWith(&params, binding.JSON)

	openid := c.GetString("openid")
	err := service.User{}.Save(
		c.Request.Context(),
		openid, params.NickName, params.Phone,
		params.Gender,
		params.Birth, params.Address, params.Email, params.Hobby, params.Image,
		params.UserName, params.TelNumber, params.PostalCode, params.ProvinceName,
		params.CityName, params.CountyName, params.StreetName, params.DetailAddress,
	)
	response.JSON(c, nil, err)
}

// 获取万象积分的用户信息
func (User) GetWanXiangMember(c *gin.Context) {
	openid := c.GetString("openid")
	data, err := service.User{}.GetWanXiangMember(c.Request.Context(), openid)
	response.JSON(c, data, err)

}

// 获取万象积分的用户信息
func (User) GetWanXiangPoints(c *gin.Context) {
	openid := c.GetString("openid")
	mallcode := c.GetString("mallcode")
	data, err := service.User{}.GetWanXiangPoints(c.Request.Context(), openid, mallcode)
	response.JSON(c, data, err)

}

// 回退万象积分
func (User) ReverseWanXiangPoints(c *gin.Context) {

	logx.Info(c.Request.Context(), "ReverseWanXiangPoints controller", logx.Any("", ""))
	var params struct {
		OrderID      string  `json:"order_id"`      // 订单号
		Points       float64 `json:"points"`        // 积分
		PointsID     string  `json:"points_id"`     // 积分类型
		PayUnivalent float64 `json:"pay_univalent"` // 抵扣值
		MallCode     string  `json:"mall_code"`     // 商场ID

	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	openid := c.GetString("openid")
	data, err := service.User{}.ReverseWanXiangPoints(c.Request.Context(), params.OrderID, openid, params.Points, params.PointsID, params.PayUnivalent, params.MallCode)
	response.JSON(c, data, err)

}
